package com.siyoumi.app.modules.app_ess.service;

import com.siyoumi.app.entity.EssClass;
import com.siyoumi.app.entity.EssClassUser;
import com.siyoumi.app.modules.app_ess.vo.EssAddStudent;
import com.siyoumi.app.modules.app_ess.vo.VaEssClass;
import com.siyoumi.app.service.EssClassService;
import com.siyoumi.app.service.EssClassUserService;
import com.siyoumi.component.XApp;
import com.siyoumi.component.XSpringContext;
import com.siyoumi.component.http.InputData;
import com.siyoumi.component.http.XHttpContext;
import com.siyoumi.exception.EnumSys;
import com.siyoumi.mybatispuls.JoinWrapperPlus;
import com.siyoumi.service.IWebService;
import com.siyoumi.util.XReturn;
import com.siyoumi.util.XStr;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//班级
@Slf4j
@Service
public class SvcEssClass
        implements IWebService {
    static public SvcEssClass getBean() {
        return XSpringContext.getBean(SvcEssClass.class);
    }

    static public EssClassService getApp() {
        return EssClassService.getBean();
    }


    public EssClassUser getEntityClassUser(String uid) {
        JoinWrapperPlus<EssClassUser> query = listUserQuery();
        query.eq("ecu_uid", uid);

        return EssClassUserService.getBean().first(query);
    }

    public List<Map<String, Object>> getList() {
        JoinWrapperPlus<EssClass> query = listQuery();
        query.select("eclass_id", "eclass_name");
        return getApp().getMaps(query);
    }

    public JoinWrapperPlus<EssClass> listQuery() {
        return listQuery(InputData.getIns());
    }

    /**
     * select
     *
     * @return query
     */
    public JoinWrapperPlus<EssClass> listQuery(InputData inputData) {
        String name = inputData.input("name");

        JoinWrapperPlus<EssClass> query = getApp().join();
        query.eq("eclass_x_id", XHttpContext.getX())
                .eq("eclass_del", 0);
        if (XStr.hasAnyText(name)) { //名称
            query.like("eclass_name", name);
        }

        return query;
    }

    public JoinWrapperPlus<EssClassUser> listUserQuery() {
        return listUserQuery(InputData.getIns());
    }

    public JoinWrapperPlus<EssClassUser> listUserQuery(String classId) {
        InputData inputData = InputData.getIns();
        inputData.put("class_id", classId);
        return listUserQuery(inputData);
    }

    public JoinWrapperPlus<EssClassUser> listUserQuery(InputData inputData) {
        String classId = inputData.input("class_id");

        JoinWrapperPlus<EssClassUser> query = EssClassUserService.getBean().join();
        query.eq("ecu_x_id", XHttpContext.getX());

        if (XStr.isNullOrEmpty(classId)) {
            query.eq("ecu_class_id", classId);
        }

        return query;
    }

    /**
     * 删除标签ID所有关系
     *
     * @param classId
     */
    public XReturn delClassUser(String classId) {
        JoinWrapperPlus<EssClassUser> query = listUserQuery(classId);
        List<EssClassUser> list = EssClassUserService.getBean().get(query);
        if (list.size() > 0) {
            List<String> ids = list.stream().map(item -> item.getKey()).collect(Collectors.toList());
            EssClassUserService.getBean().delete(ids);
        }
        return EnumSys.OK.getR();
    }

    public XReturn edit(InputData inputData, VaEssClass vo) {
        List<String> ignoreField = new ArrayList<>();
        if (inputData.isAdminEdit()) {
            ignoreField.add("eclass_acc_id");
        }

        return XApp.getTransaction().execute(status -> {
            XReturn r = getApp().saveEntity(inputData, vo, true, ignoreField);
            EssClassUser entity = r.getData("entity");
            if (vo.getTeacher_set() == 1) {
                //删除关系
                delClassUser(entity.getEcu_class_id());
                //添加权限
                List<EssClassUser> list = new ArrayList<>();
                for (String uid : vo.getTeacher_uids()) {
                    EssClassUser entityUser = new EssClassUser();
                    entityUser.setEcu_x_id(XHttpContext.getX());
                    entityUser.setEcu_class_id(entity.getKey());
                    entityUser.setEcu_uid(uid);
                    entityUser.setAutoID();

                    list.add(entityUser);
                }
                EssClassUserService.getBean().saveBatch(list);
            }

            return r;
        });
    }

    /**
     * 删除
     */
    @SneakyThrows
    @Transactional(propagation = Propagation.MANDATORY)
    public XReturn delete(List<String> ids) {
        XReturn r = XReturn.getR(0);

        //判断是否存在学生
        getApp().delete(ids);

        return r;
    }

    /**
     * 添加学生
     *
     * @param vo
     */
    @SneakyThrows
    @Transactional(rollbackFor = Exception.class)
    public XReturn addStudent(EssAddStudent vo) {
        List<EssClassUser> list = new ArrayList<>();
        for (String uid : vo.getUids()) {
            EssClassUser entity = new EssClassUser();
            entity.setEcu_uid(uid);
            entity.setEcu_class_id(vo.getClass_id());
            entity.setEcu_x_id(XHttpContext.getX());
            entity.setAutoID();

            list.add(entity);
        }

        EssClassUserService.getBean().saveBatch(list);

        return EnumSys.OK.getR();
    }
}
